//
//  LeftSortsViewController.m
//  PianKe
//
//  Created by lanou3g on 15/10/13.
//  Copyright (c) 2015年 张文文. All rights reserved.
//

#import "LeftSortsViewController.h"
#import "AppDelegate.h"
#import "otherViewController.h"
#import "MyViewController.h"
#import "MusicViewController.h"
#import "FindViewController.h"
#import "MoreViewController.h"
#import "SearchViewController.h"
#import "LoginViewController.h"
#import "SetViewController.h"

@interface LeftSortsViewController ()<UITableViewDataSource, UITableViewDelegate>
@property(nonatomic,strong)LoginViewController *loginVC;
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)UIButton *button;
@end

@implementation LeftSortsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //背景图片
    UIImageView *imageview = [[UIImageView alloc] initWithFrame:self.view.bounds];
    imageview.image = [UIImage imageNamed:@"1160492023deb0a726o.jpg"];
    //imageview.backgroundColor = [UIColor blueColor];
    [self.view addSubview:imageview];
    
    //tableview
    UITableView *tableview = [[UITableView alloc] init];
    self.tableview = tableview;
    tableview.frame = self.view.bounds;
    tableview.dataSource = self;
    tableview.delegate  = self;
    tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tableview];
    
    //登录
    UIImage *image = [UIImage imageNamed:@"iconfont-yonghutouxiang.png"];
    self.imageView = [[UIImageView alloc] initWithImage:image];
    self.imageView.frame = CGRectMake(10, 60, 75, 75);
    self.imageView.userInteractionEnabled = YES;
    [self.imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imageViewClick:)]];
    [self.view addSubview:self.imageView];
    self.button = [[UIButton alloc] initWithFrame:CGRectMake(85, 85, 100, 30)];
    [self.button setTitle:@"登录/注册" forState:UIControlStateNormal];
    //绑定事件
    [self.button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:self.button];
//
//    //功能栏
//      //下载
//    UIButton *button1 = [[UIButton alloc] initWithFrame:CGRectMake(20, 100, 30, 30)];
//    [button1 setImage:[UIImage imageNamed:@"18.png"] forState:UIControlStateNormal];
//    [self.view addSubview:button1];
//      //喜爱
//    UIButton *button2 = [[UIButton alloc] initWithFrame:CGRectMake(80, 100, 30, 30)];
//    [button2 setImage:[UIImage imageNamed:@"12.png"] forState:UIControlStateNormal];
//    [self.view addSubview:button2];
//    
//      //对话
//    UIButton *button3 = [[UIButton alloc] initWithFrame:CGRectMake(140, 100, 30, 30)];
//    [button3 setImage:[UIImage imageNamed:@"14.png"] forState:UIControlStateNormal];
//    [self.view addSubview:button3];
//      //评论
//    UIButton *button4 = [[UIButton alloc] initWithFrame:CGRectMake(200, 100, 30, 30)];
//    [button4 setImage:[UIImage imageNamed:@"17.png"] forState:UIControlStateNormal];
//    [self.view addSubview:button4];
//    
//    //搜索栏
//    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(37, 146, 200, 30)];
//    searchBar.backgroundImage = [UIImage imageNamed:@"8"];
//    searchBar.placeholder = @"搜索";
//    [self.view addSubview:searchBar];
    
}
#pragma mark - 实现button点击事件
-(void)buttonAction:(UIButton *)sender{
    [self.button setTitleColor:[UIColor purpleColor] forState:UIControlStateHighlighted];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
    if ([self.button.titleLabel.text isEqualToString:@"登录/注册"]) {
        [tempAppDelegate.mainNavigationController pushViewController:loginVC animated:YES];
    }else{
        MyViewController *myVC = [[MyViewController alloc] init];
        [tempAppDelegate.mainNavigationController pushViewController:myVC animated:YES];
    }
}

#pragma mark - 实现imageview点击事件
-(void)imageViewClick:(UITapGestureRecognizer *)sender
{   AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉

//    [self.imageView.image isEqual:[UIImage imageNamed:@"iconfont-yonghutouxiang.png"]]
    
    if ([self.button.titleLabel.text isEqualToString:@"登录/注册"]) {
        [tempAppDelegate.mainNavigationController pushViewController:loginVC animated:YES];
    }else{
        //修改头像
        //弹框
        UIAlertController *actionsheet = [UIAlertController alertControllerWithTitle:@"智能提示" message:@"请选择相机或者相册" preferredStyle:UIAlertControllerStyleActionSheet];
        //事件
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            //imagePicker.delegate = self;
            [self.navigationController presentViewController:imagePicker animated:YES completion:nil];
            
            
        }];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            if (UIImagePickerControllerSourceTypeCamera) {
                
                UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
                imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
                [self.navigationController presentViewController:imagePicker animated:YES completion:nil];
            } else {
                TRLog(@"傻了吧~~这是模拟器啊! !");
            }
            
            
        }];
        UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
        [actionsheet addAction:action1];
        [actionsheet addAction:action2];
        [actionsheet addAction:action3];
        [self.navigationController presentViewController:actionsheet animated:YES completion:nil];

    }
}

#pragma mark - 代理方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:CGRectMake(145, 117, 85, 114)];
    [self.imageView addSubview:imageV];
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 6;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"Identifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.textLabel.font = [UIFont systemFontOfSize:20.0f];
    cell.backgroundColor = [UIColor clearColor];
    cell.textLabel.textColor = [UIColor whiteColor];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的";
    } else if (indexPath.row == 1) {
        cell.textLabel.text = @"音乐馆";
    } else if (indexPath.row == 2) {
        cell.textLabel.text = @"发现";
    } else if (indexPath.row == 3) {
        cell.textLabel.text = @"搜索";
    } else if (indexPath.row == 4) {
        cell.textLabel.text = @"登录";
    } else if (indexPath.row == 5) {
        cell.textLabel.text = @"设置";
    }
    return cell;
}

//cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    AppDelegate *tempAppDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if(indexPath.row == 0){
        MyViewController *myVC = [[MyViewController alloc] init];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNavigationController pushViewController:myVC animated:NO];
    } else if (indexPath.row == 1){
        MusicViewController *musicVC = [[MusicViewController alloc] init];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNavigationController pushViewController:musicVC animated:NO];
    } else if (indexPath.row == 2){
        FindViewController *findVC = [[FindViewController alloc] init];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNavigationController pushViewController:findVC animated:NO];
    } else if (indexPath.row == 3){
        SearchViewController *searchVC = [[SearchViewController alloc] init];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNavigationController pushViewController:searchVC animated:NO];
    } else if (indexPath.row == 4){
        LoginViewController *loginVC = [[LoginViewController alloc] init];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNavigationController pushViewController:loginVC animated:NO];
    } else if (indexPath.row == 5){
        SetViewController *setVC = [[SetViewController alloc] init];
        [tempAppDelegate.LeftSlideVC closeLeftView];//关闭左侧抽屉
        [tempAppDelegate.mainNavigationController pushViewController:setVC animated:NO];
        
    }
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 180;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.tableview.bounds.size.width, 180)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

//创建音乐栏
-(void)createMusicview{
    self.musicView = [[UIView alloc] initWithFrame:CGRectMake(0, 300, 375, 50)];
    self.musicView.backgroundColor = [UIColor redColor];
    [self.tableview addSubview:self.musicView];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
